package com.shenmazong.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.pojo.TbSeller;
import com.shenmazong.service.TbSellerService;
import com.shenmazong.mapper.TbSellerMapper;
import com.shenmazong.state.ApprovalStatus;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.SellerCheckVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class TbSellerServiceImpl extends ServiceImpl<TbSellerMapper, TbSeller>
    implements TbSellerService{

    @Autowired
    TbSellerMapper tbSellerMapper;

    @Override
    public ResultResponse sellerCheck(SellerCheckVo sellerCheckVo) {
        TbSeller tbSeller = tbSellerMapper.selectById(sellerCheckVo.getSellerId());
        if(tbSeller == null) {
            return ResultResponse.FAILED(404, "商家不存在");
        }

        // 如果是审批通过
        if(sellerCheckVo.getStatus() == ApprovalStatus.YES.getCode()) {
            tbSeller.setStatus(ApprovalStatus.YES.getCode());
            tbSellerMapper.updateById(tbSeller);

            // 发送审核通过消息
            // todo:发送rabbitmq消息

            return ResultResponse.SUCCESS();
        }

        // 审核不通过：发送消息通知商家
        // todo

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse sellerLock(SellerCheckVo sellerCheckVo) {
        TbSeller tbSeller = tbSellerMapper.selectById(sellerCheckVo.getSellerId());
        if(tbSeller == null) {
            return ResultResponse.FAILED(404, "商家不存在");
        }

        // 锁定
        if(sellerCheckVo.getStatus() == ApprovalStatus.LOCK.getCode()) {
            tbSeller.setStatus(ApprovalStatus.LOCK.getCode());
            tbSellerMapper.updateById(tbSeller);

            // 发送账号锁定消息
            // todo:发送rabbitmq消息

            return ResultResponse.SUCCESS();
        }

        // 解锁消息：发送消息通知商家
        tbSeller.setStatus(ApprovalStatus.YES.getCode());
        tbSellerMapper.updateById(tbSeller);

        // todo

        return ResultResponse.SUCCESS();
    }
}




